package com.xiaohao.communication.daoImpl;

import com.xiaohao.communication.bean.UsersBaseInfo;
import com.xiaohao.communication.bean.WxOpenId;
import com.xiaohao.communication.dao.UserBaseInfoDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class UserBaseInfoDaoImpl implements UserBaseInfoDao {


    @Autowired
    MongoTemplate mongoTemplate;

    /*
     * 微信登录
     *
     * */
    @Override
    public void wxLogin(WxOpenId wxOpenId) {
        mongoTemplate.insert(wxOpenId);
    }

    /*
    * 用户登录
    * */
    @Override
    public List<UsersBaseInfo> loginUser(String username) {

        List<UsersBaseInfo> usersBaseInfo = mongoTemplate.find(new Query(
                        new Criteria().where("username").is(username)).with(PageRequest.of(0, 1)),
                UsersBaseInfo.class);
        return usersBaseInfo;
    }


    //用户注册
    @Override
    public void registerUser(UsersBaseInfo usersBaseInfo) {
        UsersBaseInfo save = mongoTemplate.save(usersBaseInfo);
        System.out.println(save);
    }

    //查询用户所有信息
    @Override
    public List<UsersBaseInfo> getUserInfo(int id) {
        Query query = new Query(Criteria.where("user_id").is(id));
        List<UsersBaseInfo> distinct = mongoTemplate.find(new Query(Criteria.where("user_id").is(id)),  UsersBaseInfo.class,"usersBaseInfo");
        return distinct;
    }

    //获取所有用户信息
    @Override
    public List<UsersBaseInfo> getAllUserInfo() {
        Query query = new Query();
        List<UsersBaseInfo> usersBaseInfo = mongoTemplate.find(query, UsersBaseInfo.class, "usersBaseInfo");
        return usersBaseInfo;
    }


    //模糊查询用户
    @Override
    public List<UsersBaseInfo> getUserInfoByVagueUsername(String username) {
        Query query = new Query(Criteria.where("username").regex(username));
        List<UsersBaseInfo> usersBaseInfo = mongoTemplate.find(query, UsersBaseInfo.class, "usersBaseInfo");
        return usersBaseInfo;
    }


}
